var express = require('express');
var router = express.Router();
let { listModel, AuctionOrders , infoModel , taocanModel,userlistModel } = require("../model/model")
var multiparty=require("multiparty") //图片上传
const alipaySdk = require('../model/aplipayUtil')
const AplipayFormData = require('alipay-sdk/lib/form').default;

router.post('/payment', (req, res, next) => {
  let orderId = req.body.orderId  //前端给后端的数据

  const formData = new AplipayFormData() //对接支付宝
 
  formData.setMethod('get')
  
  // 设置回调地址
  formData.addField('returnUrl', 'http://localhost:5173/#/pages/index/index');
  // 设置异步通知地址
  
  formData.addField('bizContent', {
    outTradeNo: orderId,
    productCode: 'FAST_INSTANT_TRADE_PAY',
    totalAmount: '100',
    subject: '商品',
    body: '商品详情',
  });

  const result = alipaySdk.exec('alipay.trade.page.pay',
    {},
    { formData: formData },
  )
  result.then((resp) => {
    res.send({
      success: 'true',
      code: 200,
      'result': resp
    })
  })

})

router.get('/getarr',async (req,res)=>{
  let pages=req.query.pages
  let page=await infoModel.find().count()
  let data=await infoModel.find().limit(4).skip((pages-1)*4)
  res.send({
    code:200,
    data,
    page
  })
})

router.get('/getinfo',async (req,res)=>{
  let _id=req.query._id
  console.log(_id);
  let data=await infoModel.findOne({_id})
  res.send({
    code:200,
    data
  })
})

router.post('/updateInfo',async (req,res)=>{
  let obj=req.body.obj
  console.log(obj.pl);
  await infoModel.updateOne({_id:obj._id},obj)
  res.send({
    code:200,
    msg:'修改成功'
  })
})


router.post("/test", async (req, res) => {
     let data = req.body
     console.log(data)
})

router.get("/list", async (req, res) => {
  let list = await listModel.find()
  res.send({
    list
  })
})

router.post("/fabu", async (req, res) => {
  let data =  req.body
  console.log(data)
  listModel.create(data)
  res.send({
    msg: "发布成功",
    code:200
  })
})

router.post("/uplike",async(req,res)=>{
  let data = req.body
  let {_id,ifLike,likes} = data
  if (ifLike==true) {
      likes++
  }else{
      likes--
  }
  await listModel.updateOne({ _id:_id }, { $set: { 'like.likes': likes, 'like.ifLike': ifLike } });
  res.send({
    code:200,
    msg:"修改点赞"
  })
})

//上传图片
router.post("/upload",(req,res)=>{
  let form=new multiparty.Form()
  form.uploadDir='upload'
  form.parse(req,(err,file,imgdata)=>{
    res.send({
       path:'http://47.93.162.7:3000/'+imgdata.file[0].path,
      //  path:'http://localhost:3000/'+imgdata.file[0].path,
    })
  })
})

router.post('/add', (req, res) => {
  const auctionOrders = new AuctionOrders(req.body);
  auctionOrders.save()
  res.send('ok')
})
router.get('/getAuctionOrdersList', async (req, res) => {
  let data = await AuctionOrders.find()
  res.send({
    data
  })
})
// 根据对应的tab标题获取数据
router.get('/getAuctionOrdersListByTabTitle', async (req, res) => {
  let { title } = req.query
  let data = []
  if (title == '全部') {
    data = await AuctionOrders.find().lean()
  } else if (title == '待付款') {
    data = await AuctionOrders.find({ tabTitle: '待付款' }).lean()
  } else if (title == '待拍摄') {
    data = await AuctionOrders.find({ tabTitle: '待拍摄' }).lean()
  } else if (title == '售后') {
    data = await AuctionOrders.find({ tabTitle: '售后' }).lean()
  } else if ('已结束') {
    data = await AuctionOrders.find({ tabTitle: '已结束' }).lean()
  } else {
    data = await AuctionOrders.find().lean()
  }
  res.send({
    data
  })

}
)
// 根据_id找对应的数据
router.get('/getAuctionOrdersById', async (req, res) => {
  let { _id } = req.query
  let data = await AuctionOrders.findById(_id).lean()
  res.send({
    data
  })

}
)

//添加套餐
router.post("/addtaocan", (req, res) => {
  let taocan = req.body;
  taocanModel.create(taocan);
  res.send("ok");
});
//获取套餐
router.get("/gettaocan", async (req, res) => {
  let taocanlist = await taocanModel.find();
  res.send({
    taocanlist,
  });
});
//添加为他人购买的用户列表，不是登录的用户列表
router.post("/adduserlist", (req, res) => {
  let user = req.body;
  console.log(user.user);
  userlistModel.create(user.user._value);
  res.send("ok");
})
//获取为他人购买的用户列表
router.get("/getuserlist", async (req, res) => {
  let userlist = await userlistModel.find();
  res.send({
    userlist,
  });
});
//删除为他人购买的用户列表中的用户
router.get("/deluserlist", async (req, res) => {
  let { _id } = req.query;
 await userlistModel.deleteOne({_id});
  res.send('删除成功');
})
//修改为他人购买的用户信息
router.post("/updateuserlist", async(req, res) => {
  let user = req.body;
  console.log(user.user);
  await userlistModel.updateOne({_id:user.user._value._id},user.user._value);
  res.send("ok");
})



/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});


module.exports = router;
